package com.zlt.bkparking.controller.parking;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.zlt.bkparking.dto.RegisterDto;
import com.zlt.bkparking.dto.LoginDto;
import com.zlt.bkparking.service.BackUserService;
import com.zlt.bkparking.utils.JWTUtil;
import com.zlt.bkparking.utils.StringUtil;
import com.zlt.bkparking.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("back_user")
public class BackUserController {

    @Autowired
    private DefaultKaptcha defaultKaptcha;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private BackUserService backUserService;

    /**
     * 获取图形验证码
     * @param response
     * @return
     */
    @GetMapping("kaptcha")
    public Result kaptcha(HttpServletResponse response){
        String text = defaultKaptcha.createText();
        log.debug("验证码为{}",text);
        String uuid = StringUtil.uuid();
        response.setHeader("token",uuid);
        //将生成的文字放入redis
        redisTemplate.opsForValue().set(uuid,text,5, TimeUnit.MINUTES);
        //创建图片
        BufferedImage image = defaultKaptcha.createImage(text);
        //将图片转换成字符串
        String s = StringUtil.changeImage2String(image);
        return Result.ok("获取成功",s);
    }

    /**
     * 登录功能
     * @param token
     * @param loginDto
     * @param response
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestHeader("token") String token, LoginDto loginDto, HttpServletResponse response){
        // 数据校验
        log.debug("token为{}",token);
        log.debug("username为{},password为{},kaptcha为{}",loginDto.getUsername(),loginDto.getPassword(),loginDto.getKaptcha());
        // 首先校验验证码
        String o = (String) redisTemplate.opsForValue().get(token);
        if (o == null){
            return Result.error("验证码过期");
        }
        if (!o.equalsIgnoreCase(loginDto.getKaptcha())){
            return Result.error("验证码错误");
        }
        try {
            String login = backUserService.login(loginDto);
            response.setHeader("token",login);
            return Result.ok("登录成功",login);
        } catch (RuntimeException e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 注册功能
     * @param token
     * @param registerDto
     * @return
     */
    @PostMapping("register")
    public Result register(@RequestHeader("token") String token,@RequestBody RegisterDto registerDto){

        //校验验证码

        if (registerDto.getKaptcha()==null){
            return Result.error("请输入验证码");
        }

        String o = (String) redisTemplate.opsForValue().get(token);
        if (o == null){
            return Result.error("验证码过期");
        }
        if (!o.equalsIgnoreCase(registerDto.getKaptcha())){
            return Result.error("验证码错误");
        }
        //校验输入数据
        if(registerDto.getBuNickname()==null){
            return Result.error("请输入昵称");
        }
        if (registerDto.getBuPassword()==null){
            return Result.error("请输入密码");
        }
        if (registerDto.getBuPhone()==null){
            return Result.error("请输入手机号");
        }
        if (registerDto.getBuUsername()==null){
            return Result.error("请输入用户名");
        }

        String s = backUserService.addBackUserByDto(registerDto);
        if("注册成功".equals(s)){
            return Result.ok(s);
        }else {
            return Result.error(s);
        }

    }

    /**
     * 注销用户
     * @return
     */
    @DeleteMapping("del_back_user")
    public Result delBackUser(@RequestHeader("token") String token){
        String username = JWTUtil.getUsername(token);

        return backUserService.setStateByUsername(username)?Result.ok("注销成功"):Result.error("注销失败");
    }

}
